/*-----------------------------------------------------------------------------
 *
 *  ntsl.c - NTSysLog main
 *
 *    Copyright (c) 1998, SaberNet.net - All rights reserved
 *
 *    This program is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation; either version 2
 *    of the License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307
 *
 *  $Id: ntsl.c,v 1.1.1.1 2001/11/28 05:25:06 sabernet Exp $
 *
 *	Options:
 *	  _DEBUG     -  Turns on memory leak detection 
 *    NTSL_STUB  -  Minimal functions for module testing
 *
 *  Revision history:
 *    17-Aug-98  JRR  Module completed
 *
 *----------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h> 
#include <sys/stat.h>
#include <direct.h>
#include <process.h>
#include <time.h>
#include "ntsl.h"
#include "service.h"
#include "eventlog.h"
#include "engine.h"


/*-------------------------------[ static data ]------------------------------*/
#define	MAX_ERROR_LEN		512
static void ntsl_cron(void *arg);


/*-----------------------------[ ntsl_log_error ]-----------------------------
 * Report error to the event log
 *----------------------------------------------------------------------------*/
void ntsl_log_error(char *format, ...)
{
	va_list     args;
    char        s[MAX_ERROR_LEN];
	
    va_start(args, format);
    _vsnprintf(s, MAX_ERROR_LEN, format, args);
    va_end(args);

	s[MAX_ERROR_LEN - 1] = 0;
	ntsl_log_msg(EVENTLOG_ERROR_TYPE, s);
}

/*------------------------------[ ntsl_log_info ]-----------------------------
 * Write information message to the event log
 *----------------------------------------------------------------------------*/
void ntsl_log_info(char *format, ...)
{
	va_list     args;
    char        s[MAX_ERROR_LEN];
	
    va_start(args, format);
    _vsnprintf(s, MAX_ERROR_LEN, format, args);
    va_end(args);

	s[MAX_ERROR_LEN - 1] = 0;
	ntsl_log_msg(EVENTLOG_INFORMATION_TYPE, s);	
}

/*----------------------------[ ntsl_log_warning ]----------------------------
 * Write information message to the event log
 *----------------------------------------------------------------------------*/
void ntsl_log_warning(char *format, ...)
{
	va_list     args;
    char        s[MAX_ERROR_LEN];
	
    va_start(args, format);
    _vsnprintf(s, MAX_ERROR_LEN, format, args);
    va_end(args);

	s[MAX_ERROR_LEN - 1] = 0;
	ntsl_log_msg(EVENTLOG_WARNING_TYPE, s);
}

/*------------------------------[ ntsl_log_msg ]-----------------------------
 * Write message to the event log
 *----------------------------------------------------------------------------*/
void ntsl_log_msg(uint16 etype, char *format, ...)
{
    va_list     args;
    char        s[MAX_ERROR_LEN];
	
    va_start(args, format);
    _vsnprintf(s, MAX_ERROR_LEN, format, args);
    va_end(args);

	s[MAX_ERROR_LEN - 1] = 0;

	#ifndef _DEBUG
	{
		HANDLE		hEvent = RegisterEventSource(NULL, APP_NAME);
		char		*strings[2] = { s, NULL };

		ReportEvent(hEvent, etype, 0, 0, NULL, 1, 0, strings, NULL);
		DeregisterEventSource(hEvent);
	}
	#else
    fprintf(stderr, "%s\n", s);
	#endif
}


/*--------------------------------[ ntsl_die ]--------------------------------
 * Log error and exit.
 *----------------------------------------------------------------------------*/
void ntsl_die(char *format, ...)
{
	va_list		args;
    char        s[MAX_ERROR_LEN];
	
    va_start(args, format);
    _vsnprintf(s, MAX_ERROR_LEN, format, args);
    va_end(args);

	s[MAX_ERROR_LEN - 1] = 0;
	ntsl_log_msg(EVENTLOG_ERROR_TYPE, s);

#ifndef NTSL_STUB
	service_stop();
#else
	exit(1);
#endif
}


#ifdef _DEBUG
//#include <crtdbg.h>
#endif

/*--------------------------------[ ntsl_nit ]--------------------------------
 * Initialize sybsystems
 *----------------------------------------------------------------------------*/
void ntsl_init()
{
#ifdef _DEBUG
//	_CrtSetReportMode(_CRT_WARN,   _CRTDBG_MODE_FILE   );
//	_CrtSetReportFile(_CRT_WARN,   _CRTDBG_FILE_STDOUT );
//	_CrtSetReportMode(_CRT_ERROR,  _CRTDBG_MODE_FILE   );
//	_CrtSetReportFile(_CRT_ERROR,  _CRTDBG_FILE_STDOUT );
//	_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE   );
//	_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT );
#endif

	engine_init();
	eventlog_init();	
	event_init();	
}


/*--------------------------------[ ntsl_run ]--------------------------------
 * Service event loop
 *----------------------------------------------------------------------------*/
void ntsl_run()
{
	ntsl_log_info(NTSL_INFO_SERVICE, "started");

	do
	{
#ifdef _DEBUG
		printf("\nScanning event logs...\n");
#endif

		eventlog_check_events();

#ifdef _DEBUG
		printf("\nSleeping...\n");
#endif
		if (!service_halting())
			Sleep(20000);
	} while(!service_halting());

	ntsl_shutdown();

	ntsl_log_info(NTSL_INFO_SERVICE, "shutdown");
}

/*------------------------------[ ntsl_shutdown ]------------------------------
 *  Shutdown subsystems
 *----------------------------------------------------------------------------*/
void ntsl_shutdown()
{
	engine_shutdown();

#ifdef _DEBUG
//	_CrtDumpMemoryLeaks();
#endif
}

